<template>
  <!-- @closed="editing=false" 关闭屉式菜单  重置编辑状态为不编辑 -->
  <van-action-sheet
    :value="value"
    @closed="editing = false"
    @input="$emit('input', $event)"
    title="编辑频道"
  >
    <div class="channel">
      <div class="tit">
        我的频道：
        <span class="tip">点击可进入频道</span>
        <van-button
          v-if="!editing"
          @click="editing = true"
          size="mini"
          type="info"
          plain
          >编辑</van-button
        >
        <van-button
          v-else
          @click="editing = false"
          size="mini"
          type="danger"
          plain
          >完成</van-button
        >
      </div>
      <van-grid class="van-hairline--left">
        <van-grid-item v-for="(channel, i) in myChannels" :key="channel.id">
          <span
            @click="enterChannel(i)"
            class="f12"
            :class="{ red: activeIndex === i }"
            >{{ channel.name }}</span
          >
          <van-icon
            @click="delChannel(channel.id, i)"
            v-show="editing"
            class="btn"
            name="cross"
          ></van-icon>
        </van-grid-item>
      </van-grid>
    </div>
    <!-- 可选频道 -->
    <div class="channel">
      <div class="tit">可选频道：</div>
      <van-grid class="van-hairline--left">
        <van-grid-item v-for="channel in canSelChannels" :key="channel.id">
          <span class="f12">{{ channel.name }}</span>
          <van-icon
            @click="addChannel(channel)"
            class="btn"
            name="plus"
          ></van-icon>
        </van-grid-item>
      </van-grid>
    </div>
  </van-action-sheet>
</template>
<script>
// 导入获取所有频道数据接口
import { allChannels, delChannel, addChannel } from '@/api/channel'
export default {
  props: {
    // 打开/关闭的状态
    value: {
      type: Boolean,
      default: false
    },
    // 接受我的频道数据
    myChannels: {
      type: Array,
      default: () => []
    },
    // 当前选中的频道索引
    activeIndex: {
      type: Number,
      default: 0
    }
  },
  computed: {
    // 可选频道数据
    canSelChannels () {
      return this.allChannels.filter(channel => {
        // 排除我的频道数据
        return !this.myChannels.some((myChannel) => myChannel.id === channel.id)
      })
    }
  },
  data () {
    return {
      // 编辑状态
      editing: false,
      // 所有频道数据
      allChannels: []
    }
  },
  created () {
    this.getAllChannels()
  },
  methods: {
    // 添加我的频道数据
    async addChannel (channel) {
      // console.log('当前要添加我的频道数据', channel)
      // 处理后台需要的数据格式
      const newMychannels = this.myChannels.map((item, i) => {
        return {
          id: item.id,
          seq: i
        }
      })
      // console.log(newMychannels)
      // 当前要添加的频道加入后台需要的数据中
      newMychannels.push({ ...channel, seq: newMychannels.length })
      // 把推荐数据删除
      newMychannels.shift()
      console.log(newMychannels)
      // 调用接口添加频道
      try {
        await addChannel(newMychannels)
        this.$toast.success('添加成功!')
        // 前端增加我的频道数据=>把当前点击的加进去
        this.myChannels.push({ ...channel })
      } catch (error) {
        this.$toast.fail('添加失败!')
      }
    },
    async delChannel (id, i) {
      try {
        await delChannel(id)
        this.$toast.success('删除成功!')
        // 如果删除的是当前选中数据的频道，选中状态就错了
        if (i < this.activeIndex) {
          this.$emit('update:activeIndex', this.activeIndex - 1)
        }
        //  修改父组件中的我的频道数据
        this.myChannels.splice(i, 1)
      } catch (error) {
        this.$toast.fail('删除失败!')
      }
    },
    // 切换父组件频道点击事件
    enterChannel (i) {
      // 关闭弹出层
      this.$emit('input', false)
      // 触发父组件自定义事件,修改频道索引值
      this.$emit('update:activeIndex', i)
    },
    async getAllChannels () {
      const { data } = await allChannels()
      this.allChannels = data.channels
    }

  }
}
</script>
<style lang="less" scope>
.van-popup--bottom {
  &.van-popup--round {
    border-radius: 0;
  }
}
.van-action-sheet {
  max-height: 100%;
  height: 100%;
  .van-action-sheet__header {
    background: #3296fa;
    color: #fff;
    .van-icon-close,
    .van-action-sheet__close {
      color: #fff;
    }
  }
}
.channel {
  padding: 10px;
  .tit {
    line-height: 3;
    text-align: left;
    .tip {
      font-size: 10px;
      color: #999;
    }
  }
  .van-button {
    float: right;
    margin-top: 7px;
  }
  .btn {
    position: absolute;
    bottom: 0;
    right: 0;
    background: #ddd;
    font-size: 12px;
    color: #fff;
  }
  .f12 {
    font-size: 12px;
    color: #555;
  }
  .red {
    color: red;
  }
}
</style>
